
import backend.app.app.models.db_sys_models as md
from backend.app.app.db.session_sys import session

# 查询信息
def query_all():
    try:
        all_info = session.query(md.TbUserorgFramework.Depart_ID,
                                 md.TbUserorgFramework.Depart_Name,md.TbUserorgFramework.Depart_Rank,
                                 md.TbUserorgFramework.Belong_Depart_ID,md.TbUserorgFramework.Remark).all()
        return all_info
    except:
        print('查找用户失败')
        session.rollback()
        return  False

#存库新增
def add_inf(fra_inf:list):

    save_infomation = md.TbUserorgFramework(Depart_ID=fra_inf[0],Depart_Name=fra_inf[1],
                                            Depart_Rank=fra_inf[2],Belong_Depart_ID=fra_inf[3],
                                            Remark=fra_inf[4],LegalPerson_ID=fra_inf[5])
    try:
        session.add(save_infomation)
        session.commit()
        print('添加新框架成功')
        session.close()
        return True
    except:
        print('添加新框架失败')
        session.rollback()
        return  False

#存库删除
def del_inf(fra_inf_id):
    try:
        session.query(md.TbUserorgFramework).filter(md.TbUserorgFramework.Depart_ID == fra_inf_id).delete()
        session.commit()
        print('删除框架成功')
        return True
    except:
        print('删除框架失败')
        session.rollback()
        return False

#存库更新
def updata_inf(fra_inf:list):

    try:
        session.query(md.TbUserorgFramework).filter(md.TbUserorgFramework.Depart_ID == fra_inf[0]).update(
            {md.TbUserorgFramework.Depart_ID:fra_inf[0],
             md.TbUserorgFramework.Depart_Name:fra_inf[1],
             md.TbUserorgFramework.Depart_Rank:fra_inf[2],
             md.TbUserorgFramework.Belong_Depart_ID:fra_inf[3],
             md.TbUserorgFramework.Remark:fra_inf[4],
             md.TbUserorgFramework.LegalPerson_ID:fra_inf[5]
             })
        session.commit()
        print('更新框架成功')
        return True
    except:
        print('更新框架失败')
        session.rollback()
        return False